% 1.5 linespread, GitHub zjuthesis #125
\newcommand{\linespreadonehalf}{\linespread{1.625}}
\newcommand{\linespreadsingle}{\linespread{1}}

% Set default linespread to one-half
\linespreadonehalf{}

% Enable subsubsection's numbering
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{2}

% Cover
\fancypagestyle{cover}
{
    \fancyhead{}
    \fancyfoot{}
    \renewcommand{\headrulewidth}{0pt}
}

\ifthenelse{\equal{\Degree}{undergraduate}}
{
    % Common header & footer
    \newcommand{\commonhead}
    {
        \fancyhead{}
        \fancyhead[EL]{\zihao{-5} \songti \TitleTypeName}
        \fancyhead[OR]{\zihao{-5} \songti \Title}
        \renewcommand{\headrulewidth}{0.6pt}
    }
    % Common page header & footer
    \fancypagestyle{common}
    {
        \commonhead{}
        \fancyfoot{}
        \fancyfoot[C]{\zihao{-5} \thepage}
    }
    % Previous part header & footer
    \fancypagestyle{previous}
    {
        \commonhead{}
        \fancyfoot{}
        \fancyfoot[C]{\zihao{-5}\thepage}
    }
}
{
    \renewcommand{\chaptermark}[1]{\markboth{\CTEXthechapter\quad#1}{}}
    \newcommand{\commonhead}
    {
        \fancyhead{}
        \fancyhead[L]{\zihao{-5} \songti \TitleTypeName}
        \fancyhead[R]{\zihao{-5} \songti \leftmark}
        \renewcommand{\headrulewidth}{0.6pt}
    }
    \fancypagestyle{common}
    {
        \commonhead{}
        \fancyfoot{}
        \ifthenelse{\equal{\TwoSide}{true}}{
            \fancyfoot[OR,EL]{\zihao{-5} \songti \thepage}
        }{
            \fancyfoot[C]{\zihao{-5} \songti \thepage}            
        }
    }
    \fancypagestyle{previous}
    {
        \fancyhead{}
        \fancyfoot{}
        \ifthenelse{\equal{\TwoSide}{true}}{
            \fancyfoot[OR,EL]{\zihao{-5} \songti \thepage}
        }{
            \fancyfoot[C]{\zihao{-5} \songti \thepage}            
        }
        \renewcommand{\headrulewidth}{0pt}
    }
}

% Explicitly remove plain page style
\fancypagestyle{plain}
{
    \fancyhead{}
    \fancyfoot{}
    \renewcommand{\headrulewidth}{0pt}
}

% Set default page styles
\pagestyle{common}
\tocloftpagestyle{previous}

% TOC side pane fixup
\bookmarksetup{
  numbered,
  open
}

% TOC set up list of algorithms to use `tocloft` style
% See: https://tex.stackexchange.com/a/147199
\makeatletter
\begingroup
  \let\newcounter\@gobble
  \let\setcounter\@gobbletwo
  \globaldefs\@ne
  \let\c@loadepth\@ne
  \newlistof{algotocloft}{loa}{\listalgorithmname}
\endgroup
\let\l@algocf\l@algotocloft
\let\l@algorithm\l@algotocloft
\let\l@algorithms\l@algotocloft
\makeatother
\let\cftalgotocloftleader\cftsecleader

% TOC title format
\renewcommand{\cfttoctitlefont}{\hfill\zihao{3}\bfseries}\renewcommand{\cftaftertoctitle}{\hfill}
\renewcommand{\cftloftitlefont}{\hfill\zihao{3}\bfseries}\renewcommand{\cftafterloftitle}{\hfill}
\renewcommand{\cftlottitlefont}{\hfill\zihao{3}\bfseries}\renewcommand{\cftafterlottitle}{\hfill}
\renewcommand{\cftloatitlefont}{\hfill\zihao{3}\bfseries}\renewcommand{\cftafterloatitle}{\hfill}

\setlength{\cftbeforetoctitleskip}{0pt}
\setlength{\cftaftertoctitleskip}{0pt}

\setlength{\cftbeforeloftitleskip}{0pt}
\setlength{\cftafterloftitleskip}{0pt}

\setlength{\cftbeforelottitleskip}{0pt}
\setlength{\cftafterlottitleskip}{0pt}

\setlength{\cftbeforeloatitleskip}{0pt}
\setlength{\cftafterloatitleskip}{0pt}

% TOC entry format
\cftpagenumbersoff{part}

\renewcommand{\cftchapfont}         {\zihao{-4}}
\renewcommand{\cftchapleader}       {\cftdotfill{\cftchapdotsep}}
\renewcommand{\cftchappagefont}     {}
\renewcommand{\cftsecfont}          {\zihao{-4}}
\renewcommand{\cftsubsecfont}       {\zihao{-4}}
\renewcommand{\cftsubsubsecfont}    {\zihao{-4}}

\renewcommand{\cftfigdotsep}        {0.5}
\renewcommand{\cfttabdotsep}        {0.5}
\renewcommand{\cftchapdotsep}       {0.5}
\renewcommand{\cftsecdotsep}        {0.5}
\renewcommand{\cftsubsecdotsep}     {0.5}
\renewcommand{\cftsubsubsecdotsep}  {0.5}

\renewcommand{\cftchapnumwidth}       {1.5em}
\renewcommand{\cftsecnumwidth}        {1.5em}
\renewcommand{\cftsubsecnumwidth}     {1.5em}
\renewcommand{\cftsubsubsecnumwidth}  {1.5em}

\setlength{\cftchapindent}      {0em}
\setlength{\cftsecindent}       {2em}
\setlength{\cftsubsecindent}    {4em}
\setlength{\cftsubsubsecindent} {6em}
\setlength{\cftbeforechapskip}  {0em}

% graduate template listoffigures/tables
\ifthenelse{\equal{\Degree}{graduate}}
{
    % listoffigures
    \renewcommand{\cftfigpresnum}{\figurename\enspace}
    \newlength{\listoffiguresnamelen}
    \settowidth{\listoffiguresnamelen}{\cftfigpresnum\cftfigaftersnum}
    \addtolength{\cftfignumwidth}{\listoffiguresnamelen}

    % listoftables
    \renewcommand{\cfttabpresnum}{\tablename\enspace}
    \newlength{\listoftablesnamelen}
    \settowidth{\listoftablesnamelen}{\cfttabpresnum\cfttabaftersnum}
    \addtolength{\cfttabnumwidth}{\listoftablesnamelen}

    % listofalgorithms
    \renewcommand{\cftalgotocloftpresnum}{\algotocloftname\enspace}
    \newlength{\listofalgotocloftsnamelen}
    \settowidth{\listofalgotocloftsnamelen}{\cftalgotocloftpresnum\cftalgotocloftaftersnum}
    \addtolength{\cftalgotocloftnumwidth}{\listofalgotocloftsnamelen}
}
{}

\makeatletter
\newcounter{blankpages}
\setcounter{blankpages}{0}
\newcounter{actualpage}
\setcounter{actualpage}{0}
\makeatother

\newcommand{\resetpagecounter}
{
    \setcounter{page}{1}
    \setcounter{blankpages}{0}
    \setcounter{actualpage}{1}
}

\newcommand{\minuspagecounter}
{
    \setcounter{page}{-1}
    \setcounter{blankpages}{0}
    \setcounter{actualpage}{-1}
}

\ifTrueBlankPage
    \makeatletter
    \apptocmd{\stepcounter}
    {
        \setcounter{actualpage}{\value{page}}%
        \addtocounter{actualpage}{-\value{blankpages}}%
    }{}{}

    \renewcommand{\cleardoublepage}
    {
        \clearpage
        \if@twoside
            \ifodd\value{page}
            \else
            \refstepcounter{blankpages}
            \begingroup
                \hbox{}
                \thispagestyle{empty}
                \newpage
            \endgroup
            \fi
        \fi
    }

    \let\origpagenumbering\pagenumbering

    \renewcommand{\pagenumbering}[1]
    {
        \origpagenumbering{#1}
        \renewcommand{\thepage}
        {
            \csname #1\endcsname{actualpage}
        }
    }
    \makeatother
\fi

\newcommand{\coverstyle}
{
    \pagestyle{cover}
}

\newcommand{\prevstyle}
{
    \cleardoublepage{}
    \assignpagestyle{\chapter}{previous}
    \thispagestyle{previous}
    \pagestyle{previous}
    \pagenumbering{Roman}
    \resetpagecounter{}
    \numberingstyle{}
}

\newcommand{\bodystyle}
{
    \cleardoublepage{}
    \assignpagestyle{\chapter}{common}
    \thispagestyle{common}
    \pagestyle{common}
    \pagenumbering{arabic}
    \resetpagecounter{}
}

\newcommand{\poststyle}
{
    \cleardoublepage{}
    \ifthenelse{\equal{\Degree}{graduate}}
    {
        \assignpagestyle{\chapter}{common}
        \thispagestyle{common}
        \pagestyle{common}
    }
    {
        \assignpagestyle{\chapter}{empty}
        \thispagestyle{empty}
        \pagestyle{empty}
    }
}

% Bib layout
\setlength{\bibitemsep}{0ex}
\setlength{\bibnamesep}{0ex}
\setlength{\bibinitsep}{0ex}
